<?php

/**
 * Trieda urcena na aktualizovanie zakladnych udajov o rozvrhoch, uciteloch 
 * a vyucbe v danych miestnostiach
 * 
 * @author Marek Letavay 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Update extends CI_Controller {

  function __construct() {
    parent::__construct();
    $this->load->model('update_model');
    if (!isLogged())
      redirect('login');
    $this->load->library('form_validation');
  }

  function index() {
    $this->template->set('title', 'SKaP - update');
    $data["mess"] = "Aktualizovanie údajov";
    $this->template->view("upload_form", $data);
  }

  /**
   * Metoda nacita udaje zo vstupneho XML suboru a vytvori datovu strukturu, 
   * ktora sa posle metode updateData v Update_model
   * 
   * @param string $file vstupny XML subor
   *  $data  asociativne pole:
   * array(
   *  teachers => array(
   *    id , first_name, surname, department, login
   *  ),
   *  rooms => array(
   *    nazov
   *  ),
   *  events => array(
   *    day , start_time, end_time, room, teachers_list => array()
   *  )
   * )  
   */
  function updateData($file) {
    $file = './temp/' . $file;
    if (preg_match("/^(.*)\.xml$/", $file)) {
      if (file_exists($file)) {
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($file);

        $data["teachers"] = array();
        $data["rooms"] = array();
        $data["events"] = array();

        $root = $xmlDoc->getElementsByTagName("rozvrh")->item(0);

        if ($root == NULL) {
          return FALSE;
        }

        foreach ($root->childNodes AS $item) {
          if ($item->nodeName == "ucitelia") {
            foreach ($item->childNodes AS $teacher) {
              if ($teacher->nodeType == XML_ELEMENT_NODE) {
                $user_code = $teacher->getAttribute("id");
                $first_name = $teacher->getElementsByTagName("meno")->item(0)->nodeValue;
                $surname = $teacher->getElementsByTagName("priezvisko")->item(0)->nodeValue;
                $department = $teacher->getElementsByTagName("oddelenie")->item(0)->nodeValue;
                $login = $teacher->getElementsByTagName("login")->item(0)->nodeValue;

                if ((substr($user_code, 0, 3) != "XXX") && (($first_name != " ") && ($surname != " "))) {
                  $array_teacher = array("user_code" => $user_code, "first_name" => $first_name, "surname" => $surname
                      , "department" => $department, "login" => $login);
                  array_push($data["teachers"], $array_teacher);
                }
              }
            }
          } elseif ($item->nodeName == "miestnosti") {
            foreach ($item->childNodes AS $room) {
              if ($room->nodeType == XML_ELEMENT_NODE) {
                $room_name = $room->getElementsByTagName("nazov")->item(0)->nodeValue;

                $array_room = array("room_name" => $room_name);
                array_push($data["rooms"], $array_room);
              }
            }
          } elseif ($item->nodeName == "hodiny") {
            foreach ($item->childNodes AS $event) {
              if ($event->nodeType == XML_ELEMENT_NODE) {
                $day = $this->getStringFromNumberOfDay($event->getElementsByTagName("den")->item(0)->nodeValue);
                $start_time = $event->getElementsByTagName("zaciatok")->item(0)->nodeValue;
                $end_time = $event->getElementsByTagName("koniec")->item(0)->nodeValue;
                $room = $event->getElementsByTagName("miestnost")->item(0)->nodeValue;
                $teachers_list = explode(",", $event->getElementsByTagName("ucitelia")->item(0)->nodeValue);


                $array_event = array("day" => $day, "start_time" => $start_time, "end_time" => $end_time,
                    "room" => $room, "teachers_list" => $teachers_list, "description" => "-");
                array_push($data["events"], $array_event);
              }
            }
          }
        }

        $data["message"] = "import data";

        $this->load->model("update_model");
        $result = $this->update_model->updateData($data);
      } else {
        $result = FALSE;
      }
    } else {
      $result = FALSE;
    }
    return $result;
  }

  /**
   * Metoda vrati poradove cislo dna v tyzdni
   * @param string $day den v tyzdni
   * @return string poradoveho cisla dna
   */
  function getStringFromNumberOfDay($day) {
    switch ($day) {
      case "pon":
        return "1";

      case "uto":
        return "2";

      case "str":
        return "3";

      case "stv":
        return "4";

      case "pia":
        return "5";
    }
  }

}

?>